#include "GUI.h"

void init_positions(Surface *ecran,Surface *fonds,Surface *menu_item,Surface *option_item,Surface *boite_saisie,Surface *titre_equipe,Surface *surf_mess,Surface *message,Surface *titre_points,Surface *boite_points,Surface *barre_progression)
{
  ecran->position.x=0;
  ecran->position.y=0;
  fonds->position.x=0;
  fonds->position.y=0;
  menu_item[0].position.x=220;
  menu_item[0].position.y=190;
  menu_item[1].position.x=240;
  menu_item[1].position.y=300;
  menu_item[2].position.x=265;
  menu_item[2].position.y=410;
  option_item[0].position.x=220;
  option_item[0].position.y=190;
  option_item[1].position.x=220;
  option_item[1].position.y=280;
  option_item[2].position.x=220;
  option_item[2].position.y=360;
  option_item[3].position.x=220;
  option_item[3].position.y=440;
  boite_saisie[0].position.x=40;
  boite_saisie[0].position.y=200;
  boite_saisie[1].position.x=640;
  boite_saisie[1].position.y=200;
  titre_equipe[0].position.x=50;
  titre_equipe[0].position.y=150;
  titre_equipe[1].position.x=650;
  titre_equipe[1].position.y=150;
  surf_mess->position.x=230;
  surf_mess->position.y=460;
  message->position.x=10;
  message->position.y=6;
  titre_points[0].position.x=80;
  titre_points[0].position.y=255;
  titre_points[1].position.x=680;
  titre_points[1].position.y=255;
  boite_points[0].position.x=40;
  boite_points[0].position.y=270;
  boite_points[1].position.x=640;
  boite_points[1].position.y=270;
  barre_progression[0].position.x=40;
  barre_progression[0].position.y=340;
  barre_progression[1].position.x=640;
  barre_progression[1].position.y=340;
}

void choix_options(Surface *ecran,Surface *fonds,Surface *menu_item,TTF_Font *police,SDL_Color couleur,int *taille_grille,int *choix_difficulte,int *choix_superP)
{
    SDL_BlitSurface(fonds->surface, NULL, ecran->surface, &fonds->position);
    switch(*choix_difficulte)
      {
      case 1: sprintf(diff, "Difficulte : %s", "Facile");
		   break;
      case 2: sprintf(diff, "Difficulte : %s", "Moyen");
		   break;
      case 3: sprintf(diff, "Difficulte : %s", "Difficile");
		   break;
      }
  menu_item[0].surface = TTF_RenderText_Blended(police, diff , couleur);
  SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position);
  sprintf(taille, "Taille des mots : %d", *taille_grille);
  menu_item[1].surface = TTF_RenderText_Blended(police, taille , couleur);
  SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
  if(*choix_superP==1)
	sprintf(SuperP, "Super partie : %s","Avec");
  else
	sprintf(SuperP, "Super partie : %s","Sans");
  menu_item[2].surface = TTF_RenderText_Blended(police, SuperP, couleur);
  SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
  menu_item[3].surface = TTF_RenderText_Blended(police, "Revenir au menu principal", couleur);
  SDL_BlitSurface(menu_item[3].surface, NULL, ecran->surface, &menu_item[3].position);  
}

void afficher_barre_progression(Surface *ecran,Surface *barre_progression,Surface *curseur_progression,int numero_equipe,int nbsecondes)
{
	SDL_Rect position;
	int i=0;
	position.x=7;
	position.y=8;
	barre_progression[0].surface = SDL_LoadBMP("saisie.bmp");
	barre_progression[1].surface = SDL_LoadBMP("saisie.bmp");
	    for(i=0;i<nbsecondes;i++)
    	{
    	curseur_progression->surface= SDL_LoadBMP("curseur_prog.bmp");
    	SDL_BlitSurface(curseur_progression->surface , NULL, barre_progression[numero_equipe-1].surface, &position);
    	position.x+=17;
    	}
	SDL_SetColorKey(barre_progression[0].surface, SDL_SRCCOLORKEY, SDL_MapRGB(barre_progression[0].surface->format, 0, 0, 0));
    SDL_BlitSurface( barre_progression[0].surface , NULL, ecran->surface, &barre_progression[0].position);
	SDL_SetColorKey(barre_progression[1].surface, SDL_SRCCOLORKEY, SDL_MapRGB(barre_progression[1].surface->format, 0, 0, 0));
    SDL_BlitSurface( barre_progression[1].surface , NULL, ecran->surface, &barre_progression[1].position);
 	SDL_Flip(ecran->surface); 
}    
void afficher_nbpoints(Surface *ecran,Surface *titre_points,Surface *boite_points,TTF_Font *police,SDL_Color couleur,int nbpoints[2])
{
    char points[15]="";
    char score[5]="";
    SDL_Color black={0,0,10};
    Surface surf_points;
    surf_points.position.x=60;
    surf_points.position.y=7;
    int i=0;
    strcpy(points,"Points");
    while(i<2)
    	{
    titre_points[i].surface = TTF_RenderText_Blended(police, points, couleur);
    SDL_BlitSurface(titre_points[i].surface, NULL, ecran->surface, &titre_points[i].position);
    boite_points[i].surface = SDL_LoadBMP("saisie.bmp");
    sprintf(score,"%d",nbpoints[i]);
    surf_points.surface = TTF_RenderText_Blended(police, score, black);
    SDL_BlitSurface( surf_points.surface, NULL, boite_points[i].surface, &surf_points.position);
    SDL_SetColorKey(boite_points[i].surface, SDL_SRCCOLORKEY, SDL_MapRGB(boite_points[i].surface->format, 0, 0, 0));
    SDL_BlitSurface( boite_points[i].surface , NULL, ecran->surface, &boite_points[i].position);
    i++;
    	}
    SDL_Flip(ecran->surface);
}

void afficher_message_jeu(Surface *ecran,Surface *surface_message,Surface *message,TTF_Font *police,SDL_Color couleur,int type_message,int numero_equipe,char *mot_a_trouver)
{
  char msg[50]="";
  switch(type_message)
  {
	case 1: sprintf(msg,"C'est a l'equipe %d de jouer !",numero_equipe);
			break;
	case 2 : strcpy(msg,"Entrez votre mot");
			 break;
	case 3 : strcpy(msg,"Votre mot est accepte");
			 break;
	case 4 : strcpy(msg,"La taille de votre mot est trop grande");
			 break;
	case 5 : strcpy(msg,"Mot refuse! la main passe, appuyez sur Entrer");
			 break;
	case 6 : strcpy(msg,"Bravo ! vous avez trouve le mot");
			 break;
	case 7 : sprintf(msg,"Perdu ! le mot etait : %s",mot_a_trouver);
			 break;
	case 8 : strcpy(msg,"Vous avez mis trop de temps! la main passe");
			 break;
  }
  surface_message->surface = SDL_LoadBMP("message.bmp");
  message->surface = TTF_RenderText_Blended(police, msg, couleur);
  SDL_BlitSurface(message->surface, NULL, surface_message->surface , &message->position);
  SDL_SetColorKey(surface_message->surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface_message->surface->format, 0, 0, 0));
  SDL_BlitSurface(surface_message->surface,NULL,ecran->surface,&surface_message->position);
  SDL_Flip(ecran->surface);
}

void init_case_jeu(Surface case_jeu[LIGNES_MAX][TAILLE_MAX])
{
  int i=0,j=0;
  case_jeu[0][0].position.x=240;
  case_jeu[0][0].position.y=200;
   for(i=0;i<5;i++)
      {
	for(j=0;j<9;j++)
	   {
	      case_jeu[i][j].position.x=case_jeu[0][0].position.x+(j*55);
	      case_jeu[i][j].position.y=case_jeu[i][0].position.y;
	    }
	 case_jeu[i+1][0].position.y=case_jeu[i][0].position.y+45;
      }
}

void afficher_plateau(Surface *ecran, TTF_Font *police,SDL_Color couleur,lettre mot[5][9],int choix_difficulte,int taille_grille)
{
    int i=0,j=0;
    char car[2];
    Surface lettre;
    Surface case_jeu[LIGNES_MAX][TAILLE_MAX];
    init_case_jeu(case_jeu);
    lettre.position.x=17;
    lettre.position.y=3;
    for(i=0;i<5;i++)
      {
	for(j=0;j<taille_grille;j++)
	    {
	      if(mot[i][j].bien_place==true)
		  case_jeu[i][j].surface = SDL_LoadBMP("case_bien_place.bmp");
	      else{ if(mot[i][j].appartient==true) 
			      case_jeu[i][j].surface = SDL_LoadBMP("case_mal_place.bmp");
		      else case_jeu[i][j].surface = SDL_LoadBMP("case_normale.bmp");
		      }
	      car[0]=mot[i][j].l;
	      car[1]=0;
	      lettre.surface = TTF_RenderText_Blended(police, car, couleur);
	      SDL_BlitSurface(lettre.surface, NULL, case_jeu[i][j].surface , &lettre.position);
	      SDL_BlitSurface(case_jeu[i][j].surface, NULL, ecran->surface, &case_jeu[i][j].position);
	     }
      }
    SDL_Flip(ecran->surface);
}

void init_grille(Surface *ecran, Surface *fonds, Surface *boite_saisie, Surface *titre_equipe,TTF_Font *police,TTF_Font *police_petite,SDL_Color couleur,lettre mot[5][9],int choix_difficulte,int taille_grille)
{
	Surface titre_mot;
	Surface titre_prog;
	titre_prog.position.x=80;
	titre_prog.position.y=325;
	titre_mot.position.x=90;
	titre_mot.position.y=185;
    SDL_BlitSurface(fonds->surface, NULL, ecran->surface, &fonds->position);
    afficher_plateau(ecran,police,couleur,mot,choix_difficulte,taille_grille);
    titre_equipe[0].surface = TTF_RenderText_Blended(police, "Equipe 1", couleur);
    SDL_BlitSurface(titre_equipe[0].surface, NULL, ecran->surface, &titre_equipe[0].position);
    titre_equipe[1].surface = TTF_RenderText_Blended(police, "Equipe 2", couleur);
    SDL_BlitSurface(titre_equipe[1].surface, NULL, ecran->surface, &titre_equipe[1].position);
    titre_mot.surface = TTF_RenderText_Blended(police_petite, "Mot", couleur);
    SDL_BlitSurface( titre_mot.surface, NULL, ecran->surface, &titre_mot.position);
    titre_mot.position.x=690;
    SDL_BlitSurface( titre_mot.surface, NULL, ecran->surface, &titre_mot.position);
    titre_prog.surface = TTF_RenderText_Blended(police_petite, "Temps" , couleur);
    SDL_BlitSurface(titre_prog.surface, NULL, ecran->surface, &titre_prog.position);
    titre_prog.position.x=680;
    SDL_BlitSurface(titre_prog.surface, NULL, ecran->surface, &titre_prog.position);
    boite_saisie[0].surface=SDL_LoadBMP("saisie.bmp");
    boite_saisie[1].surface=SDL_LoadBMP("saisie.bmp");
    SDL_SetColorKey(boite_saisie[0].surface, SDL_SRCCOLORKEY, SDL_MapRGB(boite_saisie[0].surface->format, 0, 0, 0));
    SDL_BlitSurface(boite_saisie[0].surface, NULL, ecran->surface, &boite_saisie[0].position);
    SDL_SetColorKey(boite_saisie[1].surface, SDL_SRCCOLORKEY, SDL_MapRGB(boite_saisie[1].surface->format, 0, 0, 0));
    SDL_BlitSurface(boite_saisie[1].surface, NULL, ecran->surface, &boite_saisie[1].position);
    SDL_Flip(ecran->surface);
}

void Select_prochaine(Surface *ecran,Surface *menu_item,TTF_Font *police,SDL_Color Selected,SDL_Color Unselected,int current,char type)
{
  if(type=='P')
  {
      switch(current)
	{
	case 0: 
	      menu_item[0].surface = TTF_RenderText_Blended(police, "Demarrer une partie", Selected);
	      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position); 
	      menu_item[1].surface = TTF_RenderText_Blended(police, "Choix des options", Unselected);
	      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
	      menu_item[2].surface = TTF_RenderText_Blended(police, "Quitter le jeu", Unselected);
	      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
	      break;
	case 1:
	      menu_item[0].surface = TTF_RenderText_Blended(police, "Demarrer une partie", Unselected);
	      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position); 
	      menu_item[1].surface = TTF_RenderText_Blended(police, "Choix des options", Selected);
	      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
	      menu_item[2].surface = TTF_RenderText_Blended(police, "Quitter le jeu", Unselected);
	      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
	      break;
	case 2:
	      menu_item[0].surface = TTF_RenderText_Blended(police, "Demarrer une partie", Unselected);
	      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position); 
	      menu_item[1].surface = TTF_RenderText_Blended(police, "Choix des options", Unselected);
	      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
	      menu_item[2].surface = TTF_RenderText_Blended(police, "Quitter le jeu", Selected);
	      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
	      break;
	}
  }
  if(type=='O')
    {
	  switch(current)
	      {
	      case 0:
		      menu_item[0].surface = TTF_RenderText_Blended(police, diff, Selected);
		      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position);
		      menu_item[1].surface = TTF_RenderText_Blended(police,  taille, Unselected);
		      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
		      menu_item[2].surface = TTF_RenderText_Blended(police, SuperP, Unselected);
		      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
		      menu_item[3].surface = TTF_RenderText_Blended(police, "Revenir au menu principal", Unselected);
		      SDL_BlitSurface(menu_item[3].surface, NULL, ecran->surface, &menu_item[3].position); 
		    break;
	      case 1: 
		      menu_item[0].surface = TTF_RenderText_Blended(police, diff, Unselected);
		      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position);
		      menu_item[1].surface = TTF_RenderText_Blended(police,  taille, Selected);
		      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
		      menu_item[2].surface = TTF_RenderText_Blended(police, SuperP, Unselected);
		      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
		      menu_item[3].surface = TTF_RenderText_Blended(police, "Revenir au menu principal", Unselected);
		      SDL_BlitSurface(menu_item[3].surface, NULL, ecran->surface, &menu_item[3].position);  
		    break;
	      case 2:
		      menu_item[0].surface = TTF_RenderText_Blended(police, diff, Unselected);
		      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position);
		      menu_item[1].surface = TTF_RenderText_Blended(police,  taille, Unselected);
		      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
		      menu_item[2].surface = TTF_RenderText_Blended(police, SuperP, Selected);
		      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
		      menu_item[3].surface = TTF_RenderText_Blended(police, "Revenir au menu principal", Unselected);
		      SDL_BlitSurface(menu_item[3].surface, NULL, ecran->surface, &menu_item[3].position); 
		    break;
	      case 3:
		      menu_item[0].surface = TTF_RenderText_Blended(police, diff, Unselected);
		      SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position);
		      menu_item[1].surface = TTF_RenderText_Blended(police,  taille, Unselected);
		      SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
		      menu_item[2].surface = TTF_RenderText_Blended(police, SuperP, Unselected);
		      SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
		      menu_item[3].surface = TTF_RenderText_Blended(police, "Revenir au menu principal", Selected);
		      SDL_BlitSurface(menu_item[3].surface, NULL, ecran->surface, &menu_item[3].position); 
		    break;
	      }
    }
  SDL_Flip(ecran->surface);
}

void init_SDL(Surface *ecran, Surface *fonds, TTF_Font **police_grosse, TTF_Font **police_moyenne, TTF_Font **police_petite,SDL_Color couleur,SDL_Color couleurfond)
{
    SDL_Rect position;
    if (SDL_Init(SDL_INIT_VIDEO) == -1) // Démarrage de la SDL. Si erreur alors...
    {
        fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); // Ecriture de l'erreur
        exit(EXIT_FAILURE); // On quitte le programme
    }
    if(TTF_Init() == -1)
    {
	fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
	exit(EXIT_FAILURE);
    }
    *police_grosse = TTF_OpenFont("Vera.ttf", 38);
    *police_moyenne = TTF_OpenFont("Vera.ttf", 24);
    *police_petite = TTF_OpenFont("Vera.ttf", 14);
    ecran->surface = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    fonds->surface = SDL_LoadBMP("motus.bmp");
    if (ecran->surface == NULL) // Si l'ouverture a échoué, on écrit l'erreur et on arrête
    {
        fprintf(stderr, "Impossible de charger le mode vidéo : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_BlitSurface(fonds->surface, NULL, ecran->surface, &fonds->position);
    SDL_WM_SetCaption("SDL Motus game", NULL);
    SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL );
    SDL_EnableUNICODE( 1 );
    SDL_Flip(ecran->surface);
}

void menu_principal(Surface *ecran,Surface *menu_item,TTF_Font *police,SDL_Color couleur)
{
    menu_item[0].surface = TTF_RenderText_Blended(police, "Demarrer une partie", couleur);
    SDL_BlitSurface(menu_item[0].surface, NULL, ecran->surface, &menu_item[0].position); 
    menu_item[1].surface = TTF_RenderText_Blended(police, "Choix des options", couleur);
    SDL_BlitSurface(menu_item[1].surface, NULL, ecran->surface, &menu_item[1].position);
    menu_item[2].surface = TTF_RenderText_Blended(police, "Quitter le jeu", couleur);
    SDL_BlitSurface(menu_item[2].surface, NULL, ecran->surface, &menu_item[2].position);
    SDL_Flip(ecran->surface);
}

void gestion_current_selected(char type,char key,int *current)
{
    switch(key)
    {
	case 'd':    if(type=='P')
			      {
				if(*current>=2)
					*current=0;
				  else *current+=1;
			      }
			else 
			{	if(type=='O')
			       {
				if(*current>=3)
					*current=0;
				  else *current+=1;
			      }
			 }
			  break;
	 case 'u' :     if(type=='P')
			      {
				if(*current<=0)
					*current=2;
				  else *current-=1;
			      }
			    else
			      {
					if(type=='O')
				      {
					if(*current<=0)
						*current=3;
					  else *current-=1;
				      }
			      }
			  break;
    }
}

void gerer_diff(int difficulte, int *taille_minimale)
 {
     switch(difficulte)
	{
	  case 1: *taille_minimale=5;
		      break;
	   case 2: *taille_minimale=6;
			break;
	   case 3: *taille_minimale=7;
			break;
	  }
}
